package com.chuanjiabao.wisdom.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.bean.FamilyTextCallbackBean;
import com.chuanjiabao.wisdom.databinding.ItemFamilyTextBinding;
import com.chuanjiabao.wisdom.listener.OnItemClickListener;
import com.chuanjiabao.wisdom.util.ToastUtil;

import java.util.List;

/**
 * Created by 86178 on 2019/8/20.
 * author mogen
 * 用心，成就一切。
 */

public class FamilyTextAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<FamilyTextCallbackBean.DataBeanX.DataBean> strings;
    private Context mcontext;
    OnItemClickListener mOnItemClickLitener;

    public FamilyTextAdapter(Context context, List<FamilyTextCallbackBean.DataBeanX.DataBean> strings) {
        this.mcontext = context;
        this.strings = strings;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemFamilyTextBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mcontext),
                R.layout.item_family_text, parent, false);
        binding.setAdapter(this);
        return new TourViewHolder(binding);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        holder.bindData(strings.get(position));
        final TourViewHolder tv = (TourViewHolder) holder;

        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<FamilyTextCallbackBean.DataBeanX.DataBean> {
        ItemFamilyTextBinding functionBinding;

        public TourViewHolder(ItemFamilyTextBinding dataBinding) {
            super(dataBinding.getRoot());
            functionBinding = dataBinding;
        }

        @Override
        public void bindData(FamilyTextCallbackBean.DataBeanX.DataBean data) {
            functionBinding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int positio) {
        return strings.size();
    }

    public void toNext(View view, FamilyTextCallbackBean.DataBeanX.DataBean bean) {
        ToastUtil.showLongToast("删除Id-->" + bean.getId());
    }
}